import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login'
import Layout from '@/views/Layout'
import Home from '@/views/Layout/Home'
import Content from '@/views/Layout/Content'
import NotFound from '@/views/NotFound'
import { getToken } from '@/utils/storage'
import User from '@/views/Layout/User'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    component: Layout,
    children: [
      { path: '/', component: Home },
      { path: 'content', component: Content },
      { path: '/user', component: User }
    ]
  },
  {
    path: '*',
    redirect: '/404'
  },

  {
    path: '/404',
    component: NotFound
  }

]



const router = new VueRouter({
  routes
})
const whiteName = ['/login', '/404']
router.beforeEach((to, from, next) => {
  console.log(to, from)
  const token = getToken()

  if (token) {
    return next()
  }
  // if (to.path === '/login' || to.path === '/404') {
  //   next()
  // }

  // 如果whiteName包含to.path就放行
  if (whiteName.includes(to.path)) {
    next()
  }
  next('/login')
})
export default router
